package com.wc.base.service.impl;

import com.wc.base.service.BaseService;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description:
 * @Author: wuchao
 * @Date: 2018/8/1
 */
@SuppressWarnings("unchecked")
public abstract class BaseServiceImpl<T,ID> implements BaseService<T,ID> {

    /**
     *
     * 功能描述: 获取数据库仓库
     *
     * @param:
     * @return:
     * @auther: wuchao
     * @date: 2018/8/1 14:18
     */
     public abstract JpaRepository getBaseRepository();
    /**
     * 功能描述: 根据id查询
     *
     * @param id
     * @param:
     * @return:
     * @auther: wuchao
     * @date: 2018/8/1 13:56
     */
    @Override
    public T findById(ID id) {
        T t = null;
        Optional<T> optional = getBaseRepository().findById(id);
        if(optional.isPresent()){
            t = optional.get();
        }
        return t;
    }

    /**
     * 功能描述: 保存
     *
     * @param
     * @param: entity
     * @return:
     * @auther: wuchao
     * @date: 2018/8/1 16:43
     */
    @Override
    public T save(T entity) {
        T  t = (T) getBaseRepository().save(entity);
        return t;
    }

    @Override
    public List<T> saveAll(List<T> list) {
        List list1 = getBaseRepository().saveAll(list);
        return list1;
    }
}
